﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace HoaDon
{
	public static class Enc
	{
		public static string Encrypt(string sEncrypt, string sKey)
		{
			try
			{
				byte[] arrKey = null;
				byte[] arrEncrypt = UTF8Encoding.UTF8.GetBytes(sEncrypt);
				MD5CryptoServiceProvider obj_Md5_Service = new MD5CryptoServiceProvider();
				arrKey = obj_Md5_Service.ComputeHash(UTF8Encoding.UTF8.GetBytes(sKey));
				TripleDESCryptoServiceProvider TdCP = new TripleDESCryptoServiceProvider();
				TdCP.Key = arrKey;
				TdCP.Mode = CipherMode.ECB;
				TdCP.Padding = PaddingMode.PKCS7;
				ICryptoTransform cTransform = TdCP.CreateEncryptor();
				byte[] arrResult = cTransform.TransformFinalBlock(arrEncrypt, 0, arrEncrypt.Length);
				return Convert.ToBase64String(arrResult, 0, arrResult.Length);
			}
			catch (System.Security.Cryptography.CryptographicException ex)
			{
				throw (new System.Exception(ex.Message, ex.InnerException));
			}
		}

		public static string Decrypt(string sDecrypt, string sKey)
		{
			try
			{
				byte[] arrKey = null;
				byte[] arrEncrypt = Convert.FromBase64String(sDecrypt);
				MD5CryptoServiceProvider obj_Md5_Service = new MD5CryptoServiceProvider();
				arrKey = obj_Md5_Service.ComputeHash(UTF8Encoding.UTF8.GetBytes(sKey));
				TripleDESCryptoServiceProvider TdCP = new TripleDESCryptoServiceProvider();
				TdCP.Key = arrKey;
				TdCP.Mode = CipherMode.ECB;
				TdCP.Padding = PaddingMode.PKCS7;
				ICryptoTransform cTransform = TdCP.CreateDecryptor();
				byte[] arrResult = cTransform.TransformFinalBlock(arrEncrypt, 0, arrEncrypt.Length);
				return UTF8Encoding.UTF8.GetString(arrResult);
			}
			catch (System.Security.Cryptography.CryptographicException ex)
			{
				throw (new System.Exception(ex.Message, ex.InnerException));
			}
		}
	}
}
